﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Bll;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;

/// <summary>
/// 监测记录结果上传。
/// </summary>
public class UploadTaskRecord : IUploadFile
{
    private DaContractTask daTask = DevFuture.Common.WorkContext.AppCtx.GetObject("DaContractTask") as DaContractTask;
    private DaTfiles datfile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;

    public UploadTaskRecord()
    {
        
    }

    #region IUploadFile 成员

    public void InitUpload(IUploadPage up, System.Collections.Specialized.NameValueCollection nvc)
    {
        #region 为产品检验项上传附件
        //给定任务的ID，给定通知标题，给定返回URL
        string strCtid = nvc["CTID"];//获取任务ID号
        string strType = nvc["TaskType"];//获取任务类型。
        up.SetConfigValue("FILETYPE", strType);

        if (String.IsNullOrEmpty(strCtid) || strCtid.Trim().Length == 0)
        {
            Utility.SiteError("缺失所必需的参数【任务编号】", up.Response);
        }
        else
        {
            up.SetConfigValue("CTID", strCtid.Trim());

            
            MoContractTask mo = new MoContractTask();

            mo.CtId = strCtid;
            daTask.GetEntityEx(ref mo);

            string strTransType = string.Empty;
            switch (strType)
            {
                case "1":
                    strTransType = "大电流检测结果记录文件";
                    break;
                case "2":
                    strTransType = "大容量检测结果记录文件";
                    break;
                default:
                    Utility.SiteError("必需的参数【产品检验项模板类型】为空。", up.Response);
                    return;
            }
            up.SetPageTitle(String.Format("为任务【{0}】上传附件({1})：",
                mo.CtId,  strTransType));
        }
        #endregion
    }

    public int Upload1(IUploadPage up, MoTfiles file, string opter_id)
    {
        MoRefFile modref = new MoRefFile();
        modref.Eid = up.GetConfigValue("CTID").ToString();
        modref.FileId = file.FileId;
        modref.RfId = SequenceKeys.GenerateReferFileID();
        try
        {
            return datfile.AddFileTask(file, modref, modref.Eid, up.GetConfigValue("FILETYPE").ToString());
            //return datfile.AddFileProduct(file, modref, modref.Eid, up.GetConfigValue("FILETYPE").ToString());
        }
        catch (DalException de)
        {
            Utility.GlobalLog.Error("添加单个文件失败!", de);
            Utility.SiteError("添加单个文件失败!", up.Response);
            return -1;
        }
    }

    public int UploadedMuliti(IUploadPage up, List<MoTfiles> files, string opter_id)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public string GetReturnUrl(IUploadPage up)
    {
        return String.Format("notice.aspx?edit&noticeid={0}", up.GetConfigValue("NOTICE_ID"));
    }

    #endregion
}
